package com.apes.framework.plugin.condition.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.condition.model.StrategyAuth;
import com.apes.framework.plugin.condition.repository.StrategyAuthRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StrategyAuthService extends DomainService {

    @Autowired
    private StrategyAuthRepository strategyAuthRepository;

    /**
     * 根据角色查询策略权限
     * @param request
     * @return
     */
    public List<Map> strategyAuthFindByRoleId(SimpleRequest request){
        JSONObject jo = request.getJO();

        List<Map> EXEConditionList = strategyAuthRepository.findAllEXECondition();
        List<Map> list = strategyAuthRepository.findbyRoleId(jo.getJSONObject("role").getString("id"));

        if(list.size()==0)return EXEConditionList;
        List<Map> mapList = new ArrayList<>();
            EXEConditionList.stream().forEach(E->{
                Map Emap = new HashMap(E);
                list.stream().forEach(k->{
                    Map Kmap = new HashMap(k);
                    if(Kmap.get("CONDITION_ID").equals(Emap.get("E_ID"))){
                        Emap.putAll(Kmap);
                    }

                });
                mapList.add(Emap);
            });

            return mapList;
        }


    /**
     *编辑策略权限
     * @param request
     * @return
     */
    public JSONObject save(SimpleRequest request){
        JSONObject jo = request.getJO();
        JSONArray jsonArray = jo.getJSONArray("strategyAuthItems");
        if(jo.getString("role")==null) throw new RuntimeException("通用角色不能为空");
        jsonArray.stream().forEach(item->{
            JSONObject jsonObject = (JSONObject)item;
            if(jsonObject!=null && jsonObject.getString("ID")!=null){
                StrategyAuth strategyAuth =  strategyAuthRepository.findOne(jsonObject.getString("ID"));
                strategyAuth.setAutApprove(jsonObject.getBoolean("AUT_APPROVE"));
                strategyAuth.setAutCreate(jsonObject.getBoolean("AUT_CREATE"));
                strategyAuth.setAutQryExec(jsonObject.getBoolean("AUT_QRY_EXEC"));
                strategyAuth.setAutStop(jsonObject.getBoolean("AUT_STOP"));
                strategyAuthRepository.saveAndFlush(strategyAuth);
            }
            if(jsonObject!=null && jsonObject.getString("ID")==null&&(jsonObject.getBoolean("AUT_APPROVE")||jsonObject.getBoolean("AUT_CREATE")||
                    jsonObject.getBoolean("AUT_QRY_EXEC")||jsonObject.getBoolean("AUT_STOP"))){
                StrategyAuth strategyAuth = new StrategyAuth();
                strategyAuth.setAutApprove(jsonObject.getBoolean("AUT_APPROVE"));
                strategyAuth.setAutCreate(jsonObject.getBoolean("AUT_CREATE"));
                strategyAuth.setAutQryExec(jsonObject.getBoolean("AUT_QRY_EXEC"));
                strategyAuth.setAutStop(jsonObject.getBoolean("AUT_STOP"));
                strategyAuth.setConditionId(jsonObject.getString("E_ID"));
                strategyAuth.setConditionName(jsonObject.getString("E_NAME"));
                strategyAuth.setRoleId(jo.getJSONObject("role").getString("id"));
                strategyAuthRepository.saveAndFlush(strategyAuth);
            }

        });
        JSONObject object = new JSONObject();
        request.setBody(request.getJO().toString());
        object.put("strategyAuthItems",strategyAuthFindByRoleId(request));
        object.put("role",jo.getJSONObject("role"));
        return object;
    }
}

